

function HTMLEnCode(str) {  
      if (str.length == 0) return "";  
      s = str.replace(/&/g,	"&gt;");  
      s = s.replace(/</g,		"&lt;");  
      s = s.replace(/>/g,		"&gt;");  
      s = s.replace(/ /g,		"&nbsp;");  
      s = s.replace(/\'/g,	"&#39;");  
      s = s.replace(/\"/g,	"&quot;");  
      s = s.replace(/\n/g,	"<br>"); 
      //alert(s); 
      return s;  
}

Ext.define('Case.Result', {
	extend:	'Ext.form.Panel',
	alias:	'case.result',
	initComponent: function() {
		var me_result = this;
		var obj = this.obj;
		var subCaseId = obj.Result[0].subCaseId;
		var caseData = this.caseData;
		var subCaseName = this.subCaseName;
		var caseId = caseData.id;
		var caseName = caseData.name;
		Ext.apply(me_result, {
			closable: false,
			frame: false,
			width: '100%',
			height: '100%',
			layout: 'border',
			items: [{
				region: 'north',
				layout: 'hbox',
				items: [{
					xtype: 'textfield',
					flex: 1,
					fieldLabel: 'Subcase',
					labelAlign: 'top',
					name: 'subCase',
					value: caseData.name + '/' + subCaseName 
				},{xtype: 'hiddenfield', flex: 0.1},{
					xtype: 'textfield',
					flex: 1.5,
					fieldLabel: 'Testresult',
					labelAlign: 'top',
					id: 'testresult',
					name: 'testresult'
				},{xtype: 'hiddenfield', flex: 0.05},{
					xtype: 'button',
					flex: 0.5,
					height: 41,
					text: 'rerun',
//					disabled: true,
					handler: function(){
						if (resultWin){
							resultWin.close();
						};
						if (reportWin != null){
							reportWin.close();
						};
						caseDetail.runSubCase(caseId, subCaseId, caseName, subCaseName);
					}
				},{xtype: 'hiddenfield', flex: 0.05
					}
				]
			},{
				region: 'center',
				layout: 'anchor',
				items: [me_result.steps()]
			}]
		});
		me_result.callParent(arguments);
		
		var params = null;
		if (caseData.subCases.length){
			for (n = 0; n < caseData.subCases.length; n++){
				if (caseData.subcases[n].id == subCaseId){
					params = caseData.subcases[n].params;
					break;
				};
			};
		} else {
			params = caseData.subcases.params;
		};
		
		var stepCount = obj.Result.length;
			activeTab = 0;
			subCaseResult = 'Mismatch in ';
			error = false;
		for (i = 0; i < stepCount; i++){
			stepId = obj.Result[i].stepId;
			status = obj.Result[i].status;
			var resultCode = obj.Result[i].result;
			time = obj.Result[i].date;
			startTime = obj.Result[i].startTime;
			endTime = obj.Result[i].endTime;
			actResponse = obj.Result[i].response;
			expResponse = obj.Result[i].expectedResponse;
			actResponse = HTMLEnCode( actResponse );
			actResponse = actResponse.replace(/&lt;@@@([\s\S]*?)@@@&gt;/gm, "<b><font color=red size=5>$1</font></b>");
			actResponse = actResponse.replace(/&lt;===([\s\S]*?)===&gt;/gm, "<b><font color=green>$1</font></b>");
			expResponse = HTMLEnCode( expResponse );
			expResponse = expResponse.replace(/&lt;@@@([\s\S]*?)@@@&gt;/gm, "<b><font color=red size=5>$1</font></b>");
			expResponse = expResponse.replace(/&lt;===([\s\S]*?)===&gt;/gm, "<b><font color=green>$1</font></b>");
			
			
			if (stepCount == 1){
				////expResponse = caseData.steps.response;
				title = caseData.steps.name;
			} else {
				////expResponse = caseData.steps[i].response;
				title = caseData.steps[i].name;
			};
			/*////
			if (params.length){
				var numberOfParams = params.length;
				for (j = 0; j < numberOfParams; j++){
					var splitter = '<%' + params[j].name + '%>';
					var expResponseArray = new Array;
					expResponseArray = expResponse.split(splitter);
					expResponse = expResponseArray.join(params[j].value);
				}
			} else {
				var numberOfParams = 1;
				var splitter = '<%' + params.name + '%>';
				var expResponseArray = new Array;
				expResponseArray = expResponse.split(splitter);
				expResponse = expResponseArray.join(params.value);
			};
			*/
			
			date = time.substring(0,10);
			time2 = time.substring(11,19);
			time = date + ", " + time2;
			date = startTime.substring(0,10);
			time2 = startTime.substring(11,19);
			startTime = date + ", " + time2;
			date = endTime.substring(0,10);
			time2 = endTime.substring(11,19);
			endTime = date + ", " + time2;
			switch(resultCode) {
			case RESULT_OK:
				stepresult = 'Success';
				break;
			case RESULT_FAIL_STATUSCODE:
				stepresult = 'Fail';
				error = true;
				break;
			case RESULT_FAIL_MISMATCH:
				stepresult = 'Mismatch';
				subCaseResult = subCaseResult + title + ', ';
				error = true;
				break;	
			default:
				alert("unknown result code: "+resultCode);
				break;
			}
			
			/*
			if (status != '200') {
				stepresult = 'Fail';
				error = true;
			}
			else
			if (actResponse == expResponse) {
				stepresult = 'Success';
			}
			else{ 
				if (activeTab == 0)	activeTab = i;
				stepresult = 'Mismatch';
				subCaseResult = subCaseResult + title + ', ';
				error = true;
				
				var actResponseArray = new Array;
				actResponseArray = actResponse.split('');
				var expResponseArray= new Array;
				expResponseArray = expResponse.split('');
				var m = expResponseArray.length;
				var actResponse2 = '';
				var counter = 0;
				if (actResponseArray.length > m) m = actResponseArray.length;
				for (k = 0; k < m; k++){
					if ((actResponseArray[k] != expResponseArray[k]) && (counter == 0)){
						actResponse2 = actResponse2 +  '@@@' + actResponseArray[k] + '@@@';
						counter++;
					} else {
						actResponse2 = actResponse2 + actResponseArray[k];
					};
				};
				actResponse = actResponse2;
				actResponse2 = '';
			};
			*/
			
			var stepPanel = Ext.create('step.result', {
				title:  title
			});		
			me_result.steps.add(stepPanel);
		};
		me_result.steps.setActiveTab(activeTab);
		
		if (!(error)) subCaseResult = 'OK';
		if (error) subCaseResult = subCaseResult.substring(0,subCaseResult.length-2) + ".";
		
		testresult = me_result.items.items[0].getComponent('testresult');
		testresult.setValue(subCaseResult);

		
		stepId = '';
		status = '';
		time = '';
		startTime = '';
		endTime = '';
		actResponse = '';
		expResponse = '';
		
	},

steps: function(){
	this.steps = Ext.create('Ext.tab.Panel', {
		id: 'steps',
		bodyStyle:'padding:5px 5px 0',
	    anchor: '100% 100%',
//		activeTab: 1,
	    bodyStyle:'padding:5px 5px 0',
	    anchor: '100% 100%'	
	});
	return this.steps;
	}
});

Ext.define('Step.Result',{
	extend: 'Ext.form.Panel',
	alias: 'step.result',

    initComponent: function() {
		var me_stepresult = this;
		Ext.apply(me_stepresult, {
		    frame:true,
		    bodyStyle:'padding:5px 5px 0',
		    layout: 'border',
		    anchor: '100% 100%',
		    fieldDefaults: {
		        labelAlign: 'left',
		        msgTarget: 'side'
		    },
		    items: [{
				xtype: 'container',
				region: 'north',
				height: 100,
				padding: 3,
				layout: 'border',
				items: [{
					xtype: 'container',
					region: 'north',
					layout: {
						type: 'hbox',
						pack: 'start',
						align: 'middle'
					},
					items:[{
						xtype: 'textfield',
						flex: 1.55,
						fieldLabel: 'Status',
						labelAlign: 'top',
						name: 'status',
						value: status
					},{xtype: 'hiddenfield', flex: 0.1},{
						xtype: 'textfield',
						flex: 1.55,
						fieldLabel: 'Stepresult',
						labelAlign: 'top',
						name: 'stepresult',
						value: stepresult
					}]
				},{
					xtype: 'container',
					region: 'center',
					layout: {
						type: 'hbox',
						pack: 'start',
						align: 'middle'
					},
					items: [{
						xtype: 'textfield',
						flex: 1,
						fieldLabel: 'Time',
						labelAlign: 'top',
						name: 'time',
						value: time
					},{xtype: 'hiddenfield', flex: 0.1},{
						xtype: 'textfield',
						flex: 1,
						fieldLabel: 'Start Time',
						labelAlign: 'top',
						name: 'starttime',
						value: startTime
					},{xtype: 'hiddenfield',flex: 0.1},{
						xtype: 'textfield',
						flex: 1,
						fieldLabel: 'End Time',
						labelAlign: 'top',
						name: 'endtime',
						value: endTime
					}]
				}]
			},{
			xtype: 'container',
				region: 'center',
				padding: 3,
				layout: {
					type: 'hbox',
					align: 'stretch',
					pack: 'start'
				},
				items: [{
					xtype: 'htmleditor',
					flex: 100,
					fieldLabel: 'Expected Result',
					labelAlign: 'top',
					name: 'expResponse',
					value: expResponse
				},{xtype: 'hiddenfield', flex: 1},{
					xtype: 'htmleditor',
					flex: 100,
					fieldLabel: 'Actual Result',
					labelAlign: 'top',
					name: 'actResponse',
					value: actResponse
				}]
			}
		    ]
		});
		me_stepresult.callParent(arguments);
		
	}	
	
})



	